package study.saas.order.service;


import org.springframework.stereotype.Service;
import study.saas.common.entity.YPageable;
import study.saas.common.repo.Repo;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.util.PageUtils;
import study.saas.common.util.StringUtil;
import study.saas.common.util.viewmodel.ApiResultV2;
import study.saas.common.util.viewmodel.PageModel;
import study.saas.order.entity.Commodity;
import study.saas.order.repo.CommodityRepo;
import study.saas.order.service.model.*;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wwy
 */
@Service
public class CommodityService {
    @Resource
    private CommodityRepo commodityRepo;

    public ApiResultV2<Commodity> findCommodity(String commodityName) {
        Commodity commodity=commodityRepo.findByCommodityName(commodityName);
        if (commodity != null && commodity.isDelete()) {
            commodity =null;
        }
        return  ApiResultV2.ok(commodity);
    }

    public ApiResultV2 addCommodity(CommodityAddDTO add) {
        Commodity commodity = commodityRepo.findByCommodityName(add.getCommodityName());
        if (commodity != null && !commodity.isDelete()) {
            return ApiResultV2.error("名称重复");
        }
        Commodity newCommodity;
        if(commodity ==null){
            newCommodity =new Commodity();
        }
        else {
            newCommodity =commodity;
            newCommodity.setIsDeleted(0);
        }
        CopyClassUtil.copyClass(add,newCommodity);
        commodityRepo.save(newCommodity);
        return ApiResultV2.ok();
    }
    public ApiResultV2 deleteCommodity(String commodityName){
        Commodity commodity =commodityRepo.findByCommodityName(commodityName);
        if(commodity ==null || commodity.isDelete()){
            return  ApiResultV2.ok();
        }
        commodity.setIsDeleted(1);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }

    public ApiResultV2 modifyCommodity(CommodityModifyDTO modify) {
        Commodity commodity =commodityRepo.findByCommodityName(modify.getCommodityName());
        if(commodity ==null ||commodity.isDelete()){
            return ApiResultV2.error("商品不存在！");
        }
        CopyClassUtil.copyClass(modify, commodity);
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }


    public ApiResultV2<PageModel<Commodity>> listCommodity(CommodityFilterDTO filter){
        YPageable pageable = PageUtils.Ypageable(filter.getPage(), filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select e from Commodity e where e.isDeleted = 0 ");
        if (!StringUtil.isEmpty(filter.getCommodityNameEq())) {
            sql.append(" and and e.commodityName = :getCommodityNameEq");
            param.put("getCommodityNameEq", filter.getCommodityNameEq());
        }
        if (!StringUtil.isEmpty(filter.getCommodityNameLike())) {
            sql.append(" and e.commodityName like :getCommodityNameLike");
            param.put("getCommodityNameLike", "%" + filter.getCommodityNameLike() + "%");
        }
        sql.append(" order by e.createTime desc,uuid desc ");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Commodity> pageModel = commodityRepo.queryPage(sql.toString(), param, Commodity.class, pageable);
        return ApiResultV2.ok(pageModel);
    }


}
